package br.com.ia.struts.action;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import br.com.enovar.trex.core.model.BaseModel;
import br.com.enovar.trex.struts.action.TRexBaseAction;
import br.com.enovar.trex.struts.action.TRexCRUDBaseAction;
import br.com.enovar.trex.struts.form.TRexBaseForm;
import br.com.enovar.trex.web.util.WebReflectionUtils;
import br.com.ia.business.ContatoBusiness;
import br.com.ia.model.Contato;
import br.com.ia.model.Empresa;
import br.com.ia.struts.form.ContatoForm;
import br.com.ia.struts.form.EmpresaForm;

public class ContatoAction extends TRexBaseAction {

	
	public ActionForward prepareInsert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		resetToken(request);
		generateToken(request);
		saveToken(request);
		ContatoForm contatoForm = (ContatoForm)form;
		contatoForm.reset(mapping, request);
		contatoForm.setState("insert");
		contatoForm.setMethod("insert");
		
		return mapping.getInputForward();
	}
	
	public ActionForward insert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ActionMessages errors = new ActionMessages();
		ActionMessages messages = new ActionMessages();

		ContatoForm contatoForm = (ContatoForm)form;
		contatoForm.setState("insert");

		// Se o token for valido
		if (isTokenValid(request, true)) {
			ContatoBusiness contatoBusiness = (ContatoBusiness)getBusinessClass(ContatoBusiness.class);
			
			Contato contato = new Contato();
			Empresa empresa = new Empresa();
			Empresa empresa2 = new Empresa();
			
			String nome = contatoForm.getNome();
			String telefone = contatoForm.getTelefone();
			String obs = contatoForm.getObs();
			Integer codigoEmpresa = contatoForm.getEmpresa().getCodigo();
			
			if(codigoEmpresa != null && codigoEmpresa.toString().trim().length() > 0){
				empresa.setCodigo(contatoForm.getEmpresa().getCodigo());
				contato.setEmpresa(empresa);
			}else{
				empresa2.setNome(contatoForm.getProperty("empresa.nome"));
			}
			
			contato.setNome(nome);
			contato.setTelefone(telefone);
			contato.setObs(obs);
					
			Serializable id = contatoBusiness.insert(contato, empresa2);
			
			request.setAttribute("inserted_id",id);

			String modelName = contato.getClass().getName();
			
			modelName = modelName.substring(modelName.lastIndexOf(".") + 1);

			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(	"success.insert", ""));
			saveMessages(request, messages);

			return prepareSearch(mapping, form, request, response);

		} else {
			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					"error.token.invalid"));
			saveErrors(request, errors);
			return mapping.getInputForward();
		}

	}
	
	public ActionForward prepareSearch(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ContatoForm contatoForm = (ContatoForm)form;
		
		resetToken(request);
		contatoForm.reset(mapping, request);
		contatoForm.setMethod("search");
		contatoForm.setState("search");
		
		return mapping.findForward("search");
	}
	
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ContatoForm contatoForm = (ContatoForm)form;
		contatoForm.setState("search");
		contatoForm.setMethod("search");
		
		ContatoBusiness contatoBusiness = (ContatoBusiness)getBusinessClass(ContatoBusiness.class);

		List list;

		if (contatoForm.getSearchMethod() == null
				|| contatoForm.getSearchMethod().trim().length() == 0) {
			list = contatoBusiness.retrieveAll();
		} else {

			String searchMethod = contatoForm.getSearchMethod();

			list = (List) WebReflectionUtils.executeMethod(request,
					searchMethod);
		}

		request.setAttribute("list", list);
		
		return mapping.findForward("search");
	}
	
	
	public ActionForward prepareUpdate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		resetToken(request);
		generateToken(request);
		saveToken(request);

		ContatoForm contatoForm = (ContatoForm)form;
		ContatoBusiness contatoBusiness = (ContatoBusiness)getBusinessClass(ContatoBusiness.class);
		Contato contato = (Contato) contatoBusiness.retrieveById(contatoForm.getCodigo());

		Empresa empresa = contato.getEmpresa();
		
		contatoForm.setCodigo(contato.getCodigo());
		contatoForm.setNome(contato.getNome());
		contatoForm.setTelefone(contato.getTelefone());
		contatoForm.setObs(contato.getObs());
		EmpresaForm empresaForm = contatoForm.getEmpresa();
		empresaForm.setCodigo(empresa.getCodigo());
		
		contatoForm.setState("update");
		contatoForm.setMethod("update");
		return mapping.getInputForward();
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ActionMessages errors = new ActionMessages();
		ActionMessages messages = new ActionMessages();

		
		Contato contato = new Contato();
		Empresa empresa = new Empresa();
		Empresa empresa2 = new Empresa();
		
		ContatoForm contatoForm = (ContatoForm)form;
		contatoForm.setState("update");
		
		Integer codigoEmpresa = contatoForm.getEmpresa().getCodigo();

		// Se o token for valido
		if (isTokenValid(request, true)) {
			
			if(codigoEmpresa != null && codigoEmpresa.toString().trim().length() > 0){
				empresa.setCodigo(contatoForm.getEmpresa().getCodigo());
				contato.setEmpresa(empresa);
			}else{
				empresa2.setNome(contatoForm.getProperty("empresa.nome"));
			}
			
			contato.setCodigo(contatoForm.getCodigo());
			contato.setNome(contatoForm.getNome());
			contato.setTelefone(contatoForm.getTelefone());
			contato.setObs(contatoForm.getObs());
			
			ContatoBusiness contatoBusiness = (ContatoBusiness)getBusinessClass(ContatoBusiness.class);
			
			contatoBusiness.update(contato, empresa2);
			
			String modelName = contato.getClass().getName();
			modelName = modelName.substring(modelName.lastIndexOf(".") + 1);

			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					"success.update", ""));
			saveMessages(request, messages);

			return prepareSearch(mapping, form, request, response);
		} else {
			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					"error.token.invalid"));
			saveErrors(request, errors);
			return mapping.getInputForward();
		}
	}
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ActionMessages messages = new ActionMessages();

		ContatoForm contatoForm = (ContatoForm) form;
		contatoForm.setState("search");
		contatoForm.setMethod("search");
		
		ContatoBusiness contatoBusiness = (ContatoBusiness)getBusinessClass(ContatoBusiness.class);
		contatoBusiness.deleteById(contatoForm.getCodigo());

		String modelName = contatoForm.getClass().getName();
		modelName = modelName.substring(modelName.lastIndexOf(".") + 1);
		modelName = modelName.replaceAll("Form", "");

		messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
				"success.delete", ""));
		saveMessages(request, messages);
		return this.search(mapping, form, request, response);
	}

	
	/*@Override
	public BaseModel copyProperties(TRexBaseForm form) {
		
		ContatoForm contatoForm = (ContatoForm)form;
		Contato contato = new Contato();
		Empresa empresa = new Empresa();
		
		Integer codigoEmpresa = contatoForm.getEmpresa().getCodigo();
		
		if(codigoEmpresa != null && codigoEmpresa.toString().trim().length() > 0){
			empresa.setCodigo(contatoForm.getEmpresa().getCodigo());
		}
		
		if(contatoForm.getEmpresa().getCodigo() == null){
			EmpresaAction e = new EmpresaAction();
			EmpresaForm emp = new EmpresaForm();
			emp.setCodigo(contatoForm.getEmpresa().getCodigo());
			emp.setNome(contatoForm.getEmpresa().getNome());
			emp.setTelefone(contatoForm.getEmpresa().getTelefone());
			e.copyProperties(emp);
		}
		
		contato.setCodigo(contatoForm.getCodigo());
		contato.setNome(contatoForm.getNome());
		contato.setTelefone(contatoForm.getTelefone());
		contato.setEmpresa(empresa);
		
		return contato;
		
	}

	@Override
	public void copyProperties(BaseModel model, TRexBaseForm form) {
		ContatoForm contatoForm = (ContatoForm)form;		
		Contato contato = (Contato)model;
		Empresa empresa = contato.getEmpresa();
		
		contatoForm.setCodigo(contato.getCodigo());
		contatoForm.setNome(contato.getNome());
		contatoForm.setTelefone(contato.getTelefone());
		EmpresaForm empresaForm = contatoForm.getEmpresa();
		empresaForm.setCodigo(empresa.getCodigo());
	}

	@Override
	public Serializable getId(	TRexBaseForm form, 
										HttpServletRequest arg1) {
		ContatoForm contatoForm = (ContatoForm)form;		
		return contatoForm.getCodigo();
	}*/

}
